/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OfficeOpenXMLInterop;

namespace Theme_Builder
{
    /// <summary>
    /// Encapsulates the editing of 3D properties
    /// </summary>
    public partial class Edit3D : UserControl
    {
        private OfficeOpenXMLInterop.CT_SolidColorFillProperties solidFillProps;

        public Edit3D()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets/Sets the CT_Shape3D associated with this control.
        /// </summary>
        public OfficeOpenXMLInterop.CT_Shape3D Shape3D
        {
            get
            {
                // Shape3D that we'll be building up to return
                CT_Shape3D shape3D = new CT_Shape3D();

                shape3D.bevelT = new CT_Bevel();

                // Set the bevel properties
                shape3D.bevelT.h = (long)Math.Round(float.Parse(this.textBoxBevelH.Text) * 12700f);
                shape3D.bevelT.w = (long)Math.Round(float.Parse(this.textBoxBevelW.Text) * 12700f);
                if (this.comboBoxBevel.SelectedItem != null)
                {
                    shape3D.bevelT.prst = (ST_BevelPresetType)this.comboBoxBevel.SelectedIndex;
                }

                // Snag the extrusion information
                float foo = float.Parse(this.textBoxExtrusionH.Text);
                if (foo != 0f)
                {
                    shape3D.extrusionH = (long)Math.Round(foo * 12700f);
                }

                // Set the material properties
                if (this.comboBoxMaterial.SelectedItem != null)
                {
                    shape3D.prstMaterial = (ST_PresetMaterialType)this.comboBoxMaterial.SelectedIndex;
                }

                // Don't set the contour color/width if there is a zero width applied
                foo = float.Parse(this.textBoxContourW.Text);
                if (foo != 0f)
                {
                    shape3D.contourW = (long)Math.Round(foo * 12700f);
                    shape3D.contourClr = new CT_Color();

                    // Parse which fill type we have defined
                    if (this.solidFillProps != null)
                    {
                        if (this.solidFillProps.schemeClr != null)
                        {
                            shape3D.contourClr.schemeClr = this.solidFillProps.schemeClr;
                        }
                        else if (this.solidFillProps.srgbClr != null)
                        {
                            shape3D.contourClr.srgbClr = this.solidFillProps.srgbClr;
                        }
                        else if (this.solidFillProps.sysClr != null)
                        {
                            shape3D.contourClr.sysClr = this.solidFillProps.sysClr;
                        }
                    }
                }

                return shape3D;
            }
            set
            {
                // Handle the bevel if there is one
                if(value.bevelT != null)
                {
                    this.textBoxBevelH.Text = ((float)value.bevelT.h / 12700f).ToString();
                    this.textBoxBevelW.Text = ((float)value.bevelT.w / 12700f).ToString();
                    this.comboBoxBevel.SelectedIndex = (int)value.bevelT.prst;
                }

                // Snag the material
                this.comboBoxMaterial.SelectedIndex = (int)value.prstMaterial;

                // Handle the extursion height
                this.textBoxExtrusionH.Text = ((float)value.extrusionH / 12700f).ToString();

                // Handle the contour information if there is one
                this.textBoxContourW.Text = ((float)value.contourW / 12700f).ToString();
                if (value.contourW != 0L)
                {
                    this.solidFillProps = new CT_SolidColorFillProperties();

                    // Parse the color information for the contour
                    if (value.contourClr != null)
                    {
                        if (value.contourClr.schemeClr != null)
                        {
                            this.solidFillProps.schemeClr = value.contourClr.schemeClr;
                        }
                        else if (value.contourClr.srgbClr != null)
                        {
                            this.solidFillProps.srgbClr = value.contourClr.srgbClr;
                        }
                        else if (value.contourClr.sysClr != null)
                        {
                            this.solidFillProps.sysClr = value.contourClr.sysClr;
                        }
                        else
                        {
                            System.Diagnostics.Debug.Assert(false);
                        }
                    }
                }
                else
                {
                    this.solidFillProps = null;
                }
            }
        }

        private void panelColor_Click(object sender, EventArgs e)
        {
            EditColorForm ecf = new EditColorForm();

            if (this.solidFillProps != null)
            {
                ecf.FillProps = this.solidFillProps;
            }

            if (ecf.ShowDialog(this) == DialogResult.OK)
            {
                this.solidFillProps = ecf.FillProps;

                this.panelColor.Invalidate();
            }
        }

        private void panelColor_Paint(object sender, PaintEventArgs e)
        {
            if (this.solidFillProps == null)
            {
                return;
            }

            Color color = Color.White;
            ColorMods cMods = new ColorMods();

            if (this.solidFillProps.schemeClr != null)
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }
            else if (this.solidFillProps.srgbClr != null)
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }
            else if (this.solidFillProps.sysClr != null)
            {
                // TODO
                System.Diagnostics.Debug.Assert(false, "Not Yet Implemented");
            }

            // Paint the panel correctly
            Graphics g = Graphics.FromHwnd(this.panelColor.Handle);

            RectangleF bounds = g.VisibleClipBounds;

            color = Converters.ModifyColor(color, cMods);

            SolidBrush sb = new SolidBrush(color);

            g.FillRectangle(sb, bounds);

            g.Dispose();
        }
    }
}
